package com.chaoyong.higo.cainiaowo;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class BaseRViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

	private SparseArray<View> views;
	private BaseRecyclerAdapter.OnItemClickListener mOnItemClickListener;

	public BaseRViewHolder(View itemView,
			BaseRecyclerAdapter.OnItemClickListener mOnItemClickListener) {
		super(itemView);
		itemView.setOnClickListener(this);

		this.mOnItemClickListener = mOnItemClickListener;
		this.views = new SparseArray<>();

	}

	public TextView getTextView(int viewId) {
		return retrieveView(viewId);
	}

	public Button getButton(int viewId) {
		return retrieveView(viewId);
	}

	public ImageView getImageView(int viewId) {
		return retrieveView(viewId);
	}

	public View getView(int viewId) {
		return retrieveView(viewId);
	}

	protected <T extends View> T retrieveView(int viewId) {
		View view = views.get(viewId);
		if (view == null) {
			view = itemView.findViewById(viewId);
			views.put(viewId, view);
		}
		return (T) view;
	}

	@Override
	public void onClick(View v) {
		if (mOnItemClickListener != null) {
			mOnItemClickListener.onItemClick(itemView, getLayoutPosition());
		}
	}

}
